package PagePackage;
import static org.testng.AssertJUnit.assertEquals;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;


public class teamsPage  extends internalPageClass{
	@FindBy (className="btn-add-new")
	public WebElement addNewTeamButton;
	
	
	@FindBy (name="name")
	public WebElement teamName;
	
	@FindBy (className="btn-search")
	public WebElement searchButton;
	
	@FindBy (name="sport")
	public WebElement sportDropdown;
	
	@FindBy (name="league")
	public WebElement leagueDropdown;
	
	@FindBy (name="school")
	public WebElement schoolDropdown;
	
public newTeamPage scrollAndClickTeam( int x, int y, String teamName) {

	
	WebElement newTeamClick=driver.findElement(By.xpath(teamName));
	 
    String code = "window.scroll(" + (newTeamClick.getLocation().x + x) + ","
                                 + (newTeamClick.getLocation().y + y) + ");";
 
    ((JavascriptExecutor)driver).executeScript(code, newTeamClick, x, y);
    ((JavascriptExecutor) driver).executeScript("arguments[0].click();", newTeamClick);
   
	 return MyPageFactory.getPage(driver, newTeamPage .class);
		 
	
	}
	
public newTeamPage clickAddNewTeamButton() {
	addNewTeamButton.click();
	return MyPageFactory.getPage(driver, newTeamPage.class);
}


public teamsPage scrollAndClickTeam2(int x, int y, String teamName) {
	 boolean present;
	    try {
	WebElement newTeamClick=driver.findElement(By.xpath(teamName));
	 
  String code = "window.scroll(" + (newTeamClick.getLocation().x + x) + ","
                              + (newTeamClick.getLocation().y + y) + ");";

  ((JavascriptExecutor)driver).executeScript(code, newTeamClick, x, y); 
  
      present = true;
   } catch (NoSuchElementException e) {
      present = false;
   }
	    assertEquals(false, present);
	 return MyPageFactory.getPage(driver, teamsPage.class);
	 
}


public teamsPage findTeambyName(String team){
	teamName.click();
	teamName.clear();
	teamName.sendKeys(team);
	
	 return MyPageFactory.getPage(driver, teamsPage.class);
}

public teamsPage clickSearch(){
searchButton.click();
return MyPageFactory.getPage(driver, teamsPage.class);
}

public teamsPage findTeambySport (String sport) {
	 Select select1 = new Select(sportDropdown);
	 select1.selectByVisibleText(sport);
	
	 return MyPageFactory.getPage(driver, teamsPage.class);
}

public teamsPage findTeambyLeague (String league) {
	 Select select1 = new Select(leagueDropdown);
	 select1.selectByVisibleText(league);
	
	 return MyPageFactory.getPage(driver, teamsPage.class);
}

public teamsPage findTeambySchool (String school) {
	 Select select1 = new Select(schoolDropdown);
	 select1.selectByVisibleText(school);
	
	 return MyPageFactory.getPage(driver, teamsPage.class);
}

/*public teamsPage noResults () {
	
	String results= driver.findElement(By.xpath("html/body/div[1]/main/section/div[2]/h1")).getText();
	assertEquals("No Results Found", results);
	 return MyPageFactory.getPage(driver, teamsPage.class);
}*/
public teamsPage noResults(){
	
	Boolean until = new WebDriverWait( driver, 30 ).until( new ExpectedCondition<Boolean>()
	        {
	            public Boolean apply( WebDriver webDriver )
	            {
	                try
	                {
	                	String results= driver.findElement(By.xpath("html/body/div[1]/main/section/div[2]/h1")).getText();
	                	assertEquals("No Results Found", results);
	                	
	                  
	                }
	                catch( AssertionError e )
	                {
	                	System.out.println( "No message, trying again" );
	                    return false;
	                }
	                System.out.println( "Message is on the screen" );
	                return true;
	                
	                
	            }
	
	
	        } );
	 return MyPageFactory.getPage(driver, teamsPage.class);}

public newTeamPage clickonFirstResult(){
driver.findElement(By.xpath("html/body/div/main/section/div[1]/div/table/tbody/tr[1]/td[1]/a")).click();
return MyPageFactory.getPage(driver, newTeamPage.class);
}

public boolean isOnThisPage(){
	return addNewTeamButton.isDisplayed();
}

/*@Override
void tryToOpen() {
	goToInternalPage2()
   .logout()
   .loginAs("victoria@krossover.com", "webmaster")
   .clickteamtab();
	
}*/
}